#define _CRT_SECURE_NO_WARNINGS
/**
 * Definition for a Node.
 * struct Node {
 *     int val;
 *     struct Node *next;
 *     struct Node *random;
 * };
 */

typedef struct Node Node;
Node* buynode(int x)
{
    Node* newnode = (Node*)malloc(sizeof(Node));
    newnode->val = x;
    newnode->next = newnode->random = NULL;
    return newnode;

}
void addnode(Node* head)
{

    Node* pcur = head;
    while (pcur)
    {
        Node* newnode = buynode(pcur->val);
        Node* next = pcur->next;

        newnode->next = next;
        pcur->next = newnode;
        pcur = next;
    }
}
void setradom(Node* head)
{
    Node* pcur = head;
    while (pcur)
    {
        Node* copy = pcur->next;
        if (pcur->random)
            copy->random = pcur->random->next;
        pcur = copy->next;
    }
}
struct Node* copyRandomList(struct Node* head) {
    if (head == NULL)
    {
        return 0;
    }
    addnode(head);
    setradom(head);
    Node* pcur = head;
    Node* phead, * ptail;
    phead = ptail = pcur->next;
    while (ptail->next)
    {
        pcur = ptail->next;
        ptail->next = pcur->next;
        ptail = ptail->next;

    }
    return phead;
}